package tr.edu.metu.is.mega.client;

import java.util.Date;

import tr.edu.metu.is.mega.client.core.AuthUtils;
import tr.edu.metu.is.mega.client.core.QueryCallback;
import tr.edu.metu.is.mega.client.core.event.SelectionEventBus;
import tr.edu.metu.is.mega.client.core.event.SelectionEventListener;
import tr.edu.metu.is.mega.client.resource.MEGAResources;
import tr.edu.metu.is.mega.common.dto.UserDTO;

import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.util.Format;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.layout.FillLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Timer;

/**
 * Panel to display session information.
 */
public class SessionInformationPanel extends LayoutContainer {

    private final DateTimeFormat formatter;

    private final Text fieldDateTime;

    /**
     * Default constructor.
     */
    public SessionInformationPanel() {
        setBorders(false);
        setLayout(new FillLayout(Orientation.VERTICAL));

        final Text fieldUser = new Text();
        fieldUser.setStyleAttribute("text-align", "center");
        fieldUser.setStyleAttribute("fontSize", "12px");
        fieldUser.setStyleAttribute("color", "#EEEEEE");
        // display current user's official name
        AuthUtils.getInstance().getCurrentUser(new QueryCallback<UserDTO>() {

            @Override
            public void onQueryResult(UserDTO result) {
                fieldUser.setText(Format.substitute(MEGAResources.RESOURCEBUNDLE.sessionInformationPanel_welcomeUser(), result.getFullName()));
            }
        });
        SelectionEventBus.getInstance().registerListener(AuthUtils.class, new SelectionEventListener<UserDTO>() {

            @Override
            public void selectionChanged(UserDTO result) {
                fieldUser.setText(Format.substitute(MEGAResources.RESOURCEBUNDLE.sessionInformationPanel_welcomeUser(), result.getFullName()));
            }
        });
        add(fieldUser);

        // Button logoutButton = new
        // Button(MEGAResources.RESOURCEBUNDLE.logout());
        // logoutButton.addSelectionListener(new
        // SelectionListener<ButtonEvent>() {
        //
        // @Override
        // public void componentSelected(ButtonEvent ce) {
        // AuthUtils.getInstance().logout();
        // Window.Location.reload();
        // }
        // });
        // logoutButton.setStyleName("button-link");

        // add(logoutButton);

        fieldDateTime = new Text("");
        fieldDateTime.setStyleAttribute("text-align", "center");
        fieldDateTime.setStyleAttribute("fontSize", "10px");
        fieldDateTime.setStyleAttribute("color", "#EEEEEE");
        add(fieldDateTime);

        formatter = DateTimeFormat.getFormat("dd.MM.yyyy HH:mm");
        Timer timer = new Timer() {

            @Override
            public void run() {
                fieldDateTime.setText(formatter.format(new Date()));
            }
        };
        timer.scheduleRepeating(1000);
        setStyleAttribute("background", "#15428B");
    }
}
